package wang.z.component;


import wang.z.JFrame.loginFrm;
import wang.z.model.ResultInfo;
import wang.z.net.FailListener;
import wang.z.net.PostUtils;
import wang.z.net.SuccessListener;
import wang.z.utils.JsonUtils;
import wang.z.utils.ResultInfoData2Vector;
import wang.z.utils.toolUtil;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Vector;

import static javax.swing.JTable.AUTO_RESIZE_OFF;

/**
 * @author like
 * @date 2022/12/13 1:38
 * @Description TODO
 */
public class AdminBorrowInfo {
    private JPanel panel_1 = new JPanel();
    private Vector<Vector> Date;//标题
    private DefaultTableModel Model;
    private JTable Table;
    private JFrame jf = null;
    private Vector<Vector> BookAllDate = new Vector<Vector>();
    private Vector<Vector> UserAllDate = new Vector<Vector>();

    public AdminBorrowInfo(JFrame jf){
        this.jf = jf;
        panel_1.setBorder(new TitledBorder(null, "数据信息", TitledBorder.LEADING, TitledBorder.TOP,
                null, new Color(255, 0, 0)));
        panel_1.setBounds(20, 20, 541, 260);
        String[] title = {"借书人", "书名", "状态", "借书时间", "还书时间"};
        Vector BookTitle = new Vector();
        for (String i : title) {
            BookTitle.add(i);
        }
        /*具体的各栏行记录 先用空的二维数组占位*/
        Date = new Vector<Vector>();

        Model = new DefaultTableModel(Date, BookTitle);
        Table = new JTable(Model) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        Table.getSelectionModel().setSelectionMode(AUTO_RESIZE_OFF);

        panel_1.setLayout(null);
        JScrollPane jscrollpane = new JScrollPane();
        jscrollpane.setBounds(20, 20, 496, 230);
        jscrollpane.setViewportView(Table);
        panel_1.add(jscrollpane);
        tableDate();
        jf.add(panel_1);
        jf.add(new JPanel());
    }

    public void tableDate() {
        FindAll();
        FindUser();
        PostUtils.post("http://localhost:8080/borrow_detail/findAlldb", new SuccessListener() {
            @Override
            public void success(String result) throws IOException {
                ResultInfo info = JsonUtils.parseResult(result);
                if (info.isFlag()) {
                    Vector<Vector> vectors = ResultInfoData2Vector.convertResultInfoData2Vector(info);
                    Vector<Vector> tranvector = dbtranvector(vectors);
                    //清空tableData的数据
                    Date.clear();

                    //类型id和书名对应
                    for (Vector vector : tranvector) {
                        for (Vector vector1 : BookAllDate) {
                            if (vector1.get(0).equals(vector.get(2))) {
                                vector.set(2, vector1.get(1));

                            }
                        }
                        vector.remove(0);
                        for (Vector vector1 : UserAllDate) {
                            if (vector1.get(0).equals(vector.get(0))) {
                                vector.set(0, vector1.get(1));

                            }
                        }
                        Date.add(vector);

                    }

                    //刷新表格
                    Model.fireTableDataChanged();
                } else {
                    JOptionPane.showMessageDialog(jf, info.getMessage());
                    if(info.getMessage().equals("用户尚未登录")){
                        new loginFrm().init();
                        jf.dispose();
                    }
                }
            }
        }, new FailListener() {
            @Override
            public void fail() {
                JOptionPane.showMessageDialog(jf, "网络异常，请稍后重试");
            }
        });
    }
    public void FindAll() {
        PostUtils.post("http://localhost:8080/book/getbylike", new SuccessListener() {
                    //请求发送成功，服务器正常响应，会执行这里
                    @Override
                    public void success(String result) throws IOException {//result参数就是服务器响应回来的json字符串
                        ResultInfo info = JsonUtils.parseResult(result);
                        //判断info中的flag标记
                        if (info.isFlag()) {
                            Vector<Vector> vectors = ResultInfoData2Vector.convertResultInfoData2Vector(info);
                            for (Vector vector : vectors) {
                                BookAllDate.add(vector);
                            }
                        } else {

                            JOptionPane.showMessageDialog(jf, info.getMessage());

                            if(info.getMessage().equals("用户尚未登录")){
                                new loginFrm().init();
                                jf.dispose();
                            }
                        }
                    }
                }
                , new FailListener() {
                    //当该请求发送失败后会执行这个方法
                    @Override
                    public void fail() {
                        JOptionPane.showMessageDialog(jf, "网络异常，请稍后重试");
                    }
                }
        );
    }
    public void FindUser() {
        PostUtils.post("http://localhost:8080/user/getbylike", new SuccessListener() {
                    //请求发送成功，服务器正常响应，会执行这里
                    @Override
                    public void success(String result) throws IOException {//result参数就是服务器响应回来的json字符串
                        ResultInfo info = JsonUtils.parseResult(result);
                        //判断info中的flag标记
                        if (info.isFlag()) {
                            Vector<Vector> vectors = ResultInfoData2Vector.convertResultInfoData2Vector(info);
                            for (Vector vector : vectors) {
                                UserAllDate.add(vector);
                            }
                        } else {

                            JOptionPane.showMessageDialog(jf, info.getMessage());

                            if(info.getMessage().equals("用户尚未登录")){
                                new loginFrm().init();
                                jf.dispose();
                            }
                        }
                    }
                }
                , new FailListener() {
                    //当该请求发送失败后会执行这个方法
                    @Override
                    public void fail() {
                        JOptionPane.showMessageDialog(jf, "网络异常，请稍后重试");
                    }
                }
        );
    }
    public static Vector<Vector> dbtranvector(Vector<Vector> vector) {
        for (Vector ve : vector) {
            ve.set(4, toolUtil.getDateByTime((ArrayList<Integer>) ve.get(4)));
            ve.set(5, toolUtil.getDateByTime((ArrayList<Integer>) ve.get(5)));
            int o = (int) ve.get(3);
            if (o == 1) {
                ve.set(3, "已归还");
            }else
            {
                ve.set(3, "未归还");
            }



        }
        return vector;
    }

}
